package com.tudouji.project.bill.controller;

import java.util.ArrayList;
import java.util.List;

import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.bill.domain.vo.ProducePlanReportVO;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.bill.domain.Producereport;
import com.tudouji.project.bill.service.IProducereportService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 生产报Controller
 * 
 * @author tudouji
 * @date 2021-03-01
 */
@RestController
@RequestMapping("/bill/producereport")
public class ProducereportController extends BaseController
{
    @Autowired
    private IProducereportService producereportService;

    @Autowired
    private ISalespermissionService salespermissionService;

    /**
     * 查询生产报列表
     */
    @PreAuthorize("@ss.hasPermi('bill:producereport:list')")
    @GetMapping("/list")
    public TableDataInfo list(Producereport producereport)
    {
        startPage();
        List<Producereport> list = producereportService.selectProducereportList(producereport);
        return getDataTable(list);
    }

    /**
     * 导出生产报列表
     */
    @PreAuthorize("@ss.hasPermi('bill:producereport:export')")
    @Log(title = "生产报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Producereport producereport)
    {
        List<Producereport> list = producereportService.selectProducereportList(producereport);
        ExcelUtil<Producereport> util = new ExcelUtil<Producereport>(Producereport.class);
        return util.exportExcel(list, "producereport");
    }

    /**
     * 模板导出报表
     */
    @Log(title = "生产报", businessType = BusinessType.EXPORT)
    @GetMapping("/exportTemple")
    public AjaxResult exportTemple(Producereport producereport)
    {
        return producereportService.export(producereport);
    }

    /**
     * 获取生产报详细信息
     */
    @PreAuthorize("@ss.hasPermi('bill:producereport:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(producereportService.selectProducereportById(id));
    }

    /**
     * 新增生产报
     */
    @PreAuthorize("@ss.hasPermi('bill:producereport:add')")
    @Log(title = "生产报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Producereport producereport)
    {
        return toAjax(producereportService.insertProducereport(producereport));
    }

    /**
     * 修改生产报
     */
    @PreAuthorize("@ss.hasPermi('bill:producereport:edit')")
    @Log(title = "生产报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Producereport producereport)
    {
        return toAjax(producereportService.updateProducereport(producereport));
    }

    /**
     * 删除生产报
     */
    @PreAuthorize("@ss.hasPermi('bill:producereport:remove')")
    @Log(title = "生产报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(producereportService.deleteProducereportByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('bill:producereport:planlist')")
    @GetMapping("/planlist")
    public TableDataInfo planlist(ProducePlanReportVO producePlanReportVO)
    {
        List<ProducePlanReportVO> list = new ArrayList<ProducePlanReportVO>();
        int flag = 0;
        if(StringUtils.isNotEmpty(SecurityUtils.getLoginUser().getUser().getTuserId())){

            List<Salespermission> permissions = this.salespermissionService.getPermByScAndFunc(SecurityUtils.getLoginUser().getUser().getTuserId(), "8", "4");
            if (permissions.size() < 1) {
                permissions = this.salespermissionService.getPermByScAndFunc(SecurityUtils.getLoginUser().getUser().getTuserId(), "8", "7");
                if (permissions.size() > 0) {
                    flag = 1;
                    String[] srmids = new String[permissions.size()];
                    for (int i = 0; i < permissions.size(); i++) {
                        srmids[i] = permissions.get(i).getSrguid();
                    }
                    producePlanReportVO.setSrmids(srmids);
                }
            }else{
                flag = 1;
            }
            if(flag == 1){
                startPage();
                list = producereportService.producePlanReport(producePlanReportVO);
            }

        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('bill:producereport:exportplanlist')")
    @Log(title = "导出生产计划报表", businessType = BusinessType.EXPORT)
    @GetMapping("/exportplanlist")
    public AjaxResult exportplanlist(ProducePlanReportVO producePlanReportVO)
    {
        List<ProducePlanReportVO> list = new ArrayList<ProducePlanReportVO>();
        if(StringUtils.isNotEmpty(SecurityUtils.getLoginUser().getUser().getTuserId())){
            List<Salespermission> permissions = this.salespermissionService.getPermByScAndFunc(SecurityUtils.getLoginUser().getUser().getTuserId(), "8", "4");
            if (permissions.size() == 0) {
                permissions = this.salespermissionService.getPermByScAndFunc(SecurityUtils.getLoginUser().getUser().getTuserId(), "8", "7");
                if (permissions.size() > 0) {
                    String[] srmids = new String[permissions.size()];
                    for (int i = 0; i < permissions.size(); i++) {
                        srmids[i] = permissions.get(i).getSrguid();
                    }
                    producePlanReportVO.setSrmids(srmids);
                }
            }
            //startPage();
            list = producereportService.producePlanReport(producePlanReportVO);
        }
        ExcelUtil<ProducePlanReportVO> util = new ExcelUtil<ProducePlanReportVO>(ProducePlanReportVO.class);
        return util.exportExcel(list, "ProducePlanReportVO");
    }
}
